$ ->
  $dialog = $("#user_edit_password")
  $dialog.delegate "form", "submit", (e) ->
    e.preventDefault()

    $form = $(e.currentTarget)
    $success = $(".alert-success", $form)
    $error = $(".alert-warning", $form)

    old_password = $("input.old_password", $form).val()
    password = $("input.password", $form).val()
    password_confirmation = $("input.password_confirmation", $form).val()
    

    if $.isEmptyObject(password)
      $error.html("新密码为空！")
      $error.show()
      return 
    

    if not password_confirmation is password
      $error.html("新密码与确认密码不一致！")
      $error.show();
      return 
    

    $.ajax
      url: $form.attr("action")
      data: $form.serialize()
      type: "put"
      dataType: "json"
      success: (e) ->
        $error.hide();
        $success.html("修改成功")
        $success.show()
        $dialog.modal("hide")
        $form[0].reset()

      error: (e) ->
        $success.hide()
        $error.html(JSON.parse(e.responseText).join(','))
        $error.show();
  .on "click", ".save", ->
    $("form", $dialog).submit()
